
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
    <el-form-item label="优惠券类型">
          <el-radio-group v-model="data.type">
              <el-radio :label="0">通用券</el-radio>
              <el-radio :label="1">品类券</el-radio>
              <el-radio :label="2">商品券</el-radio>
          </el-radio-group>
    </el-form-item>
      <el-form-item label="商品" prop="product_id"   v-if="data.type == 2">
        <el-select v-model.trim="data.product_id" filterable multiple  placeholder="请选择商品" clearable  class="filter-item" >
            <el-option  v-for="(item,index) in goods" :key="index" :label="item.store_name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="category_id"   v-if="data.type == 1">
        <el-select v-model.trim="data.category_id" filterable placeholder="请选择分类" clearable  class="filter-item" >
            <el-option  v-for="(item,index) in category" :key="index" :label="item.cate_name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="优惠券名称" prop="title">
        <el-input v-model.trim="data.title" placeholder="请填写标题" />
    </el-form-item>
     <el-form-item label="优惠券图片" prop="thumb" >
            <el-upload
        :action="$store.state.settings.file_upload_url"
        :headers ="headerObj"
        :limit=1
        list-type="picture-card"
        :file-list="data.thumbfileList"
        accept="image/png, image/jpeg, image/gif, image/jpg"
        :on-success="handlethumbSuccess"
        :before-upload="beforethumbUpload"
        :on-exceed="handlethumbExceed"
        :on-preview="handlethumbPreview"
        :on-remove="handlethumbRemove">
        <i class="el-icon-plus"></i>
        </el-upload>
        </el-form-item>
            <el-form-item label="优惠券面值" prop="coupon_price">
                <el-input v-model.trim="data.coupon_price" placeholder="请填写优惠券面值" />
            </el-form-item>
            <el-form-item label="最低消费" prop="use_min_price">
                <el-input v-model.trim="data.use_min_price" placeholder="请填写最低消费" />
                <span style="color:#999">最低消费多少金额可用优惠券</span>
            </el-form-item>
              <el-form-item label="效期限（天）" prop="coupon_time">
                  <el-input v-model.trim="data.coupon_time" placeholder="请填写效期限（天）" />
              </el-form-item>
              <el-form-item label="排序" prop="sort">
                  <el-input-number v-model="data.sort" :min="1" :max="10" label="排序"></el-input-number>
              </el-form-item>
              <el-form-item label="状态">
                  <el-switch :active-value="1" :inactive-value="0" active-text="开启" inactive-text="关闭" v-model="data.status"></el-switch>
              </el-form-item>

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/coupontemplate'
import { getToken } from '@/utils/auth'


export default {
  name: 'coupontemplateCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    goods: {
      type: Array,
      default: () => {
        return []
      }
    },
    category: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    title: [ 
                { required: true, message: "请输入标题", trigger: "blur" }
            ], 
            coupon_price: [ 
                { required: true, message: "请输入优惠券面值", trigger: "blur" }
            ], 

      },
      data: {
                    title:'',
            coupon_price:'',
            use_min_price:'',
            coupon_time:'',
            sort:'',
            status:1,
            product_id:'',
            category_id:'',
            type:0,
            thumb:[],
            thumbpreviewurl:'',
            thumbfileList:[],

      }
    }
  },
  created() {
  },
  methods: {
    
                handlethumbExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handlethumbPreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handlethumbRemove(){
                 this.data.thumb = '';
             },
             handlethumbSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.thumb= response.data.src;
            },
             beforethumbUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传图片大小不能超过 2MB!");
                }
                
            },

    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create({
                       title:this.data.title,
            coupon_price:this.data.coupon_price,
            use_min_price:this.data.use_min_price,
            coupon_time:this.data.coupon_time,
            sort:this.data.sort,
            status:this.data.status ?1:0,
            product_id: this.data.product_id ? this.data.product_id.join(',') : '',
            category_id:this.data.category_id,
            type:this.data.type,
            thumb:this.data.thumb,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
